package world.snowcrystal.service.Impl;

import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import world.snowcrystal.exception.SnowcrystalFailToWriteMailException;
import world.snowcrystal.exception.SnowcrystalInvalidMailException;
import world.snowcrystal.properties.AuthenticationProperties;
import world.snowcrystal.service.MailService;

@Service
public class TencentMailService implements MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private Engine tempalteEngine;

    @Autowired
    private AuthenticationProperties authenticationProperties;
    private  Template defaultTemplate ;



    /**
     * 发送邮件给指定收件人
     *
     * @param to   收件人应是一个邮箱地址
     * @param text 指定内容，可以是 html 文本
     */
    @Override
    public void sendMail(String to, String text,boolean useHtml)
            throws SnowcrystalInvalidMailException, SnowcrystalFailToWriteMailException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try{
            helper.setTo(new InternetAddress(to));
        }catch (MessagingException e){
            throw new SnowcrystalInvalidMailException(e);
        }
        try{
            helper.setText(text,useHtml);
        }catch (MessagingException e){
            throw new SnowcrystalFailToWriteMailException(e);
        }




    }

    /**
     * 使用默认的 HTML 模板发送验证码，调用者需要自行验证目的邮箱的合法性
     *
     * @param to               目的邮箱
     * @param verificationCode 验证码
     * @author tianqing
     * @since 1.0
     */
    @Override
    public void sendMail(String to, String verificationCode) {
        Template template = retrieveTemplate("default");
        int sec = authenticationProperties.getMailVerificationExpirationTime() / 60;
        String text = template.renderToString(Kv.of("verificationCode", verificationCode).set("expirationTime",sec));
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try{
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from);
            helper.setSubject("注册验证码");
            helper.setTo(to);
            helper.setText(text,true);
        }catch (MessagingException e){
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);

    }

    /**
     * 使用默认的 HTML 模板向用户发送密码，调用者需要自行验证目的邮箱的
     * 合法性
     *
     * @author tianqing
     * @since 1.0
     */
    @Override
    public void sendMailWithDefaultPassword(String to, String password) {
        Template template = retrieveTemplate("password");
        String text = template.renderToString(Kv.of("password", password));
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try{
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from);
            helper.setSubject("你的默认密码");
            helper.setTo(to);
            helper.setText(text,true);
        }catch (MessagingException e){
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);

    }

    Template retrieveTemplate(String templateName) {
        return tempalteEngine.getTemplate("/mail/"+templateName+".html");
    }
}
